package com.example.myone.Mapper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.AbstractWindowedCursor;
import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteCursorDriver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQuery;
import android.os.Build;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.example.myone.Entity.Music;
import com.example.myone.R;
import com.example.myone.Util.BytesUtils;
import com.example.myone.sql.MySQLiteHelper;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MusicMapper {
    private static final String TABLE_NAME = "music";

    // 插入一个音乐
    public static Music insertMusic(Context context, Music music) {
        SQLiteDatabase db = MySQLiteHelper.getWritableDatabase(context);
        ContentValues values = new ContentValues();
        values.put("article_id", music.articleId);
        values.put("bytes", music.bytes);
        long insert = db.insert(TABLE_NAME, null, values);
        music.id = (int) insert;
        return music;
    }

    // 根据文章ID获取音乐
    public static Music getMusicByArticleId(Context context, Integer articleId) {
        SQLiteDatabase database = MySQLiteHelper.getReadableSQLiteDatabase(context);
        Cursor cursor = database.rawQuery(
                "SELECT * FROM " + TABLE_NAME + " WHERE article_id = ?",
                new String[]{articleId.toString()});
        CursorWindow cursorWindow = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            cursorWindow = new CursorWindow("music", 30000000);
        }
        AbstractWindowedCursor abstractWindowedCursor = (AbstractWindowedCursor) cursor;
        abstractWindowedCursor.setWindow(cursorWindow);
        List<Music> musics = getMusics(cursor);
        return musics.isEmpty() ? null : musics.get(0);
    }

    // 检查 music 表是否有数据
    private static boolean isTableEmpty(Context context) {
        SQLiteDatabase database = MySQLiteHelper.getReadableSQLiteDatabase(context);
        try (Cursor cursor = database.rawQuery("select count(*) from " + TABLE_NAME, null)) {
            cursor.moveToFirst();
            return cursor.getInt(0) == 0;
        }
    }

    // 初始化方法
    public static void init(Context context) {
        if (!isTableEmpty(context)) return;
        System.out.printf("==> 初始化 %s 数据库数据", TABLE_NAME);
        Music music = new Music();
        music.articleId = 1;
        InputStream musicStream = context.getResources().openRawResource(R.raw.home);
        music.bytes = BytesUtils.readAllBytes(Objects.requireNonNull(musicStream));
        insertMusic(context, music);
    }

    @SuppressLint("Range")
    public static ArrayList<Music> getMusics(Cursor cursor) {
        ArrayList<Music> musics = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Music music = new Music();
                music.id = cursor.getInt(cursor.getColumnIndex("id"));
                music.articleId = cursor.getInt(cursor.getColumnIndex("article_id"));
                music.bytes = cursor.getBlob(cursor.getColumnIndex("bytes"));
                musics.add(music);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return musics;
    }

    // 根据 ID 删除音乐
    public static int deleteMusic(Context context, int id) {
        SQLiteDatabase db = MySQLiteHelper.getWritableDatabase(context);
        return db.delete(TABLE_NAME, "id=?", new String[]{String.valueOf(id)});
    }
}


